package sdk.pay.utils;

import android.text.TextUtils;

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * Created by Administrator on 2016/5/16 0016.
 * code fromat and modify codes by Joe on 2017/5/27
 */
public final class PayAESUtil {
    private static final String LOG_TAG = "PayAESUtil ";
    private static Cipher mCipher = null;
    private static final String CHAR_SET_NAME = "utf-8";
    private static final String KEY = "66af8593f42b01b3ee815ad8be980e3c";
    private static final String VECTOR = "04c52bed86e11f9e";

    private static void log(String str) {
        String logStr = LOG_TAG + str;
        PayLogUtil.logFourth(logStr);
        //save log
        PayExceptionUtil.getInstance().saveExceptionFile(logStr);
    }

    static {
        try {
            mCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            log("static NoSuchAlgorithmException = " + e.getMessage());
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
            log("static NoSuchPaddingException = " + e.getMessage());
        }
    }

    /**
     * @param content  encrypt content
     * @param password key
     * @param iv       vector
     * @return byte[]
     */
    public static byte[] encrypt(String content, String password, String iv) {
        try {
            SecretKeySpec key = new SecretKeySpec(password.getBytes(CHAR_SET_NAME), "AES");
            IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes(CHAR_SET_NAME));
            byte[] byteContent = content.getBytes(CHAR_SET_NAME);
            mCipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
            return mCipher.doFinal(byteContent);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
            log("encrypt InvalidKeyException = " + e.getMessage());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            log("encrypt UnsupportedEncodingException = " + e.getMessage());
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
            log("encrypt IllegalBlockSizeException = " + e.getMessage());
        } catch (BadPaddingException e) {
            e.printStackTrace();
            log("encrypt BadPaddingException = " + e.getMessage());
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
            log("encrypt InvalidAlgorithmParameterException = " + e.getMessage());
        }
        return null;
    }

    /**
     * @param content  decrypt content
     * @param password key
     * @param iv       vector
     * @return byte[]
     */
    public static byte[] decrypt(byte[] content, String password, String iv) {
        if (TextUtils.isEmpty(password) || TextUtils.isEmpty(iv)) {
            password = KEY;
            iv = VECTOR;
        }
        try {
            SecretKeySpec key = new SecretKeySpec(password.getBytes(CHAR_SET_NAME), "AES");
            IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes(CHAR_SET_NAME));
            mCipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
            return mCipher.doFinal(content);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
            log("decrypt InvalidKeyException = " + e.getMessage());
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
            log("decrypt IllegalBlockSizeException = " + e.getMessage());
        } catch (BadPaddingException e) {
            e.printStackTrace();
            log("decrypt BadPaddingException = " + e.getMessage());
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
            log("decrypt InvalidAlgorithmParameterException = " + e.getMessage());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            log("decrypt UnsupportedEncodingException = " + e.getMessage());
        }
        return null;
    }
}
